<!DOCTYPE html>
<html lang=zh-CN>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta property="og:description" content="samuel-24276的博客">
    <meta property="og:type" content="website">
    <meta name="description" content="samuel-24276的博客">
    <meta name="keyword"  content="aircloud">
    <link rel="shortcut icon" href="/myblog/img/favicon.ico">

    <title>
        
        C++11 in Qt5 - samuel-24276的博客 | samuel-24276&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/myblog/css/aircloud.css">

    
<link rel="stylesheet" href="/myblog/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_28hi1hpxx24.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 7.1.1"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> Like what you do, do what you like </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/myblog/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>samuel-24276</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/myblog/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/myblog/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/myblog/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/myblog/collect/">
                    <i class="iconfont icon-shoucang1"></i>
                    <span>收藏</span>
                </a>
            </li>
            <li >
                <a href="/myblog/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Generic-lambda"><span class="toc-text">Generic lambda</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Unicode-literal"><span class="toc-text">Unicode literal</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C-11%E5%85%B3%E9%94%AE%E5%AD%97%E4%B8%8EQt%E5%AE%8F"><span class="toc-text">C++11关键字与Qt宏</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#constexpr"><span class="toc-text">constexpr</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#static-assert"><span class="toc-text">static_assert</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#override%E4%B8%8Efinal"><span class="toc-text">override与final</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#delete"><span class="toc-text">delete</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%B3%E5%80%BC%E5%BC%95%E7%94%A8%E4%B8%8E%E7%A7%BB%E5%8A%A8%E8%AF%AD%E4%B9%89"><span class="toc-text">右值引用与移动语义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Conclusion"><span class="toc-text">Conclusion</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-bg" id="search-bg"></div>
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> Like what you do, do what you like </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        C++11 in Qt5
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2021-05-04 15:24:15</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/myblog/tags/#Qt" title="Qt">Qt</a>
        <span>/</span>
        
        <a class="tag" href="/myblog/tags/#C++" title="C++">C++</a>
        <span>/</span>
        
        <a class="tag" href="/myblog/tags/#文章翻译" title="文章翻译">文章翻译</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <blockquote>
<p>文章来源：<a target="_blank" rel="noopener" href="https://woboq.com/blog/cpp11-in-qt5.html">C++11 in Qt5</a></p>
</blockquote>
<h2 id="Generic-lambda"><a href="#Generic-lambda" class="headerlink" title="Generic lambda"></a>Generic lambda</h2><p>Lambda表达式是c++ 11中允许声明匿名函数的新语法。可以使用匿名函数来使用小函数作为参数，而不需要显式地声明它们。<br>以前在结构体中使用operator()编写仿函(functors)的方法需要大量样板代码。</p>
<p>在Qt 4.8中已经可以在一些QtConcurrent函数中使用lambda表达式。现在甚至可以使用新的连接语法将它们用作插槽。</p>
<p>还记得必须为插槽编写单行函数的时候吗?现在你可以把它写在这里了。直接引用函数的地方可读性更好:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">connect</span>(sender, &amp;Sender::valueChanged, [=](<span class="type">const</span> QString &amp;newValue) &#123;</span><br><span class="line">       receiver-&gt;<span class="built_in">updateValue</span>(<span class="string">&quot;senderValue&quot;</span>, newValue);</span><br><span class="line">   &#125;);</span><br></pre></td></tr></table></figure>
<p>Lambda函数从MSVC 2010、GCC 4.5和clang 3.1开始支持。</p>
<h2 id="Unicode-literal"><a href="#Unicode-literal" class="headerlink" title="Unicode literal"></a>Unicode literal</h2><p>在c++ 11中，你可以通过写入u”MyString”来生成UTF-16。Qt使用它来实现QStringLiteral，这是一个宏，在编译时初始化QString，没有运行时开销。<br><code>QString someString = QStringLiteral(&quot;Hello&quot;); </code></p>
<h2 id="C-11关键字与Qt宏"><a href="#C-11关键字与Qt宏" class="headerlink" title="C++11关键字与Qt宏"></a>C++11关键字与Qt宏</h2><h3 id="constexpr"><a href="#constexpr" class="headerlink" title="constexpr"></a>constexpr</h3><p>用来<strong>注释(annotate)<strong>一些内联函数，以指定它们可以</strong>在编译时计算</strong>。在Qt 5中，我们引入了<code>Q_DECL_CONSTEXPR</code>，当编译器支持它时，它被定义为constexpr。<br>我们还注释了一些有意义的Qt函数(例如QFlags)，这允许它们在常量表达式中使用。</p>
<h3 id="static-assert"><a href="#static-assert" class="headerlink" title="static_assert"></a>static_assert</h3><p>当在编译时使用static_assert检测到错误时，c++ 11帮助生成更好的错误消息。在Qt5中，我们引入了宏<code>Q_STATIC_ASSERT</code>和<code>Q_STATIC_ASSERT_X</code>，它们将在可用的情况下使用static_assert，或者在不可用的情况下使用其他模板技巧。</p>
<p>Qt已经大量使用该宏，以便在API使用不当时提供更好的编译器错误。</p>
<h3 id="override与final"><a href="#override与final" class="headerlink" title="override与final"></a>override与final</h3><p>您是否曾经因为重新实现的虚函数的名称出现了拼写错误而导致代码无法工作?(或者忘了后面那个该死的const)</p>
<p>现在，可以使用<code>Q_DECL_OVERRIDE</code>对打算覆盖虚函数的函数进行注释。</p>
<p>如果编译器支持的话，这个宏将被替换为新的”override”属性，或者其他什么都不替换。如果你用c++ 11编译器编译你的代码，如果你有一个拼写错误，或者如果你在重构时改变了一个虚方法，你会得到一个错误。</p>
<p>因为我们忘记了const，它会产生如下错误:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mymodel.h:<span class="number">15</span>: error: `Qt::ItemFlags MyModel::<span class="title function_ invoke__">flags</span>(<span class="keyword">const</span> QModelIndex&amp;)`</span><br><span class="line"> marked <span class="keyword">override</span>, but does not <span class="keyword">override</span></span><br></pre></td></tr></table></figure>

<p>还有<code>Q_DECL_FINAL</code>被替换为final属性，<strong>该属性指定虚函数不能被覆盖</strong>。</p>
<h3 id="delete"><a href="#delete" class="headerlink" title="delete"></a>delete</h3><p>对于支持删除函数的编译器，新的宏<code>Q_DECL_DELETE</code>扩展为&#x3D; delete。这有助于提供更好的编译器错误，避免常见错误。</p>
<p>**Deleted函数用于显式删除编译器自动删除的函数(如默认构造函数或复制赋值操作符)**。删除的函数可能不会被调用，如果使用了它们，则会抛出编译器错误。</p>
<p>例如，我们在<code>Q_DISABLE_COPY</code>宏中使用它。在此之前，诀窍是保持这些成员为私有，但错误消息不是很好。</p>
<h2 id="右值引用与移动语义"><a href="#右值引用与移动语义" class="headerlink" title="右值引用与移动语义"></a>右值引用与移动语义</h2><p>在Qt 4.8的文章中，我已经解释了右值引用的含义。</p>
<p><strong>由于Qt5中共享类引用计数的内部结构发生了变化，因此可以为其中许多类添加一个移动构造函数</strong>。</p>
<h2 id="Conclusion"><a href="#Conclusion" class="headerlink" title="Conclusion"></a>Conclusion</h2><p>MSVC不需要任何特殊标志，并默认启用c++ 11特性，但GCC或Clang要求-std&#x3D;c++0x。</p>
<p>默认情况下，Qt5本身将在需要它的编译器上使用c++ 11标志进行编译。</p>
<p>如果你使用qmake，你可以将这一行添加到你的.pro文件(Qt5):<br><code>CONFIG += c++11</code><br>(在Qt4中，它应该类似于:gcc:CXXFLAGS +&#x3D; -std&#x3D;c++0x)</p>
<p>现在你可以享受c++ 11的所有好特性了。(仅仅为了能够使用auto，就已经值得这么做了)</p>
<hr>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/myblog/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>
        <div id="lv-container"></div>
        <div class="giscus"></div>
    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/samuel-24276">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://github.com/samuel-24276">samuel-24276&#39;s Page</a></span>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/">北京-未备案</a></span>
        <span>/</span>
        
        <span><a href="#">返回顶部</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/myblog/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/myblog/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






    <script type="text/javascript">
       (function() {
           if (typeof LivereTower === 'function') { return; }

           var j, d = document.getElementById('lv-container');

           d.setAttribute('data-id','city');
           d.setAttribute('data-uid' , 'MTAyMC80OTA5OC8yNTU5Mw==');

           j = document.createElement('script');
           j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
           j.async = true;

           d.appendChild(j);
       })();
    </script>
    <noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
    </div>

</html>
